<?php

namespace App\Listeners;

use App\Models\User;
use Illuminate\Auth\Events\Authenticated;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

class LogAuthenticated
{
    /**
     * 登录成功后，将 remember_token 设置为 sessionId，清除原 session
     *
     * @param  Authenticated  $event
     * @return void
     */
    public function handle(Authenticated $event)
    {
        /** @var User $user */
        $user = $event->user;

        session()->getHandler()->destroy($user->remember_token);

        $user->remember_token = session()->getId();

        $user->save();
    }
}
